package com.lesso.yzh.sunflower.controller;

import com.lesso.yzh.sunflower.core.ApiResponse;
import com.lesso.yzh.sunflower.pojo.dto.DeviceDTO;
import com.lesso.yzh.sunflower.pojo.dto.DeviceQueryDTO;
import com.lesso.yzh.sunflower.pojo.dto.DeviceUpdateDTO;
import com.lesso.yzh.sunflower.pojo.vo.DeviceVO;
import com.lesso.yzh.sunflower.service.IDeviceService;
import org.apache.dubbo.common.utils.Assert;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/base/device")
public class DeviceController {
    @DubboReference
    private IDeviceService deviceService;

    @PostMapping("/save")
    public ApiResponse<Integer> save(@RequestBody DeviceDTO dto) {
        return ApiResponse.ok("success", deviceService.save(dto));
    }

    @PutMapping("/update")
    public ApiResponse<Integer> update(@RequestBody DeviceUpdateDTO dto) {
        return ApiResponse.ok("success", deviceService.update(dto));
    }

    @DeleteMapping("/delete")
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        Assert.notNull(id, "id不能为空");
        if (id < 0) {
            throw new RuntimeException("id不能小于0");
        }
        deviceService.deleteById(id);
        return ApiResponse.ok();
    }
    @GetMapping("/page")
    public ApiResponse<Page<DeviceVO>> page(DeviceQueryDTO dto){
        return ApiResponse.ok("success", (Page<DeviceVO>) deviceService.page(dto));
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<DeviceVO>> pageCustom(DeviceQueryDTO dto){
        return ApiResponse.ok("success", (Page<DeviceVO>) deviceService.customPage(dto));
    }
}
